package com.weilan.ark3.tcp;

import com.weilan.ark3.buffer.DataArray;
import com.weilan.ark3.buffer.MsgQueue;
import com.weilan.ark3.common.GlobalParam;
import com.weilan.ark3.entity.ControlDto;
import com.weilan.ark3.sqlite.ActSqliteUtil;
import com.weilan.ark3.sqlite.SQLiteParamUtils;
import com.serotonin.modbus4j.ProcessImageListener;
import com.weilan.ark3.utils.SocUtils;

import static com.weilan.ark3.buffer.OverallSituation.controlId;

/**
 * @Author: xmd
 * @Description: 从站监听器
 * @Date: 2024/5/8 17:07
 */
public class SlaveListener implements ProcessImageListener {
    private static int currentIndex;

    short[] shorts1 = new short[100];

    // 05-》写线圈的请求
    @Override
    public void coilWrite(int offset, boolean oldValue, boolean newValue) {
        System.out.println("Coil at offset " + offset + " was written. Old value: " + oldValue + " New value: " + newValue);

    }

    // 06-》写保持寄存器的请求
    @Override
    public void holdingRegisterWrite(int address, short oldValue, short value) {
        //将修改的命令添加到485命令队列中
        ControlDto controlTcp = new ControlDto(controlId, address, (int) value);
        short[] shorts = DataArray.slaveDataMap.get(1);
        if (address == 75 || address == 73 || address == 74) {
            switch (address) {
                //采集程序
                case 73:
                    shorts[73] = value;
                    shorts[51] = value;
                    TcpSlave.slaveMap.get(1).setHoldingRegister(73, (short) 1);
                    TcpSlave.slaveMap.get(1).setHoldingRegister(51, (short) 1);
                    break;
                //WebServer
                case 74:
                    shorts[52] = value;
                    shorts[74] = value;
                    TcpSlave.slaveMap.get(1).setHoldingRegister(52, (short) 1);
                    TcpSlave.slaveMap.get(1).setHoldingRegister(74, (short) 1);
                    break;
                //控制权切换（NET-0 COM2-1）
                case 75:
                    shorts[75] = value;
                    ActSqliteUtil.insertToDBAct("EMS control switch" + value + "-TCP");
                    break;
            }
        } else if (shorts[75] == 0) {

            //对地址做限定
            if (address >= 60 && address <= 100) {
                switch (address) {
                    //启停控制
                    case 61:
                        controlTcp.setAddress(301);
                        controlTcp.setValue((int) value);
                        MsgQueue.writeToControlTcp(controlTcp);
                        shorts[61] = value;
                        if (value == 2) {
                            ActSqliteUtil.insertToDBAct("The system stops-TCP");
                        } else {
                            ActSqliteUtil.insertToDBAct("The system runs-TCP");
                        }
                        break;
                    //模式切换
                    case 60:
                        SQLiteParamUtils.updateToMode(value);
                        if (value == 4) {
                            GlobalParam.fgState = 1;
                            ActSqliteUtil.insertToDBAct("Toggle peak-to-valley mode-TCP ");
                        } else if (value == 1 || value == 3 || value == 2) {
                            GlobalParam.backState = 0;
                            GlobalParam.fgState = 0;
                            GlobalParam.PVState = 0;
                            GlobalParam.socState = 0;
                            controlTcp.setAddress(300);
                            controlTcp.setValue((int) value);
                            MsgQueue.writeToControlTcp(controlTcp);
                            if (value == 1) {
                                ActSqliteUtil.insertToDBAct("Toggle the grid-connected mode-TCP ");

                            } else if (value == 2) {
                                ActSqliteUtil.insertToDBAct("Toggle the off-grid mode-TCP ");

                            } else {
                                ActSqliteUtil.insertToDBAct("Toggle the on-grid and off-grid mode-TCP ");
                            }
                            //休眠下发0功率
                            try {
                                Thread.sleep(200);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            controlTcp.setAddress(302);
                            controlTcp.setValue(0);
                            MsgQueue.writeToControlTcp(controlTcp);
                        } else if (value == 5) {
                            GlobalParam.backState = 1;
                            ActSqliteUtil.insertToDBAct("Toggle the backup mode-TCP ");
                        } else if (value == 7 || value == 9 || value == 10 || value == 12) {
                            GlobalParam.PVState = 1;
                            ActSqliteUtil.insertToDBAct("Toggle the pv mode-TCP ");
                        } else if (value == 8) {
                            GlobalParam.socState = 1;
                        }

                        shorts[60] = value;
                        break;
                    //有功功率下发
                    case 63:
                        //备电禁充时无法下发功率
                        if (GlobalParam.bansign != 1) {
                            controlTcp.setAddress(302);
                            controlTcp.setValue((int) value);
                            MsgQueue.writeToControlTcp(controlTcp);
                            ActSqliteUtil.insertToDBAct("Active Power delivery - Tcp");
                        }
                        break;
                    //无功功率下发
                    case 64:
                        controlTcp.setAddress(303);
                        controlTcp.setValue((int) value);
                        MsgQueue.writeToControlTcp(controlTcp);
                        ActSqliteUtil.insertToDBAct("Reactive Power delivery - Tcp");
                        break;
//                            SOC_H
                    case 65:
                        if (value >= 0 && value <= 100) {
                            SQLiteParamUtils.updateToSOCH(value);
                            shorts[65] = value;
                            ActSqliteUtil.insertToDBAct("Modify soc_h" + value * 0.1 + "-TCP");
                        }
                        break;
//                            SOC_L
                    case 66:
                        if (value >= 0 && value <= 100) {
                            SQLiteParamUtils.updateToSOCL(value);
                            shorts[66] = value;
                            ActSqliteUtil.insertToDBAct("Modify soc_l" + value * 0.1 + "-TCP");
                        }
                        break;
                    //备电充电功率
                    case 67:
                        SQLiteParamUtils.updateToBackPower(-value);
                        shorts[67] = (short) value;
                        ActSqliteUtil.insertToDBAct("Modify the backup power" + value + "-TCP");
                        break;
                    //并网容差
                    case 68:
                        SQLiteParamUtils.updateToAllowance(value);
                        shorts[68] = value;
                        ActSqliteUtil.insertToDBAct("Modify the allowance " + value + "-TCP");
                        break;
                    //离网容差
                    case 69:
                        SQLiteParamUtils.updateToOffAllowance(value);
                        shorts[69] = value;
                        ActSqliteUtil.insertToDBAct("Modify the allowance " + value + "-TCP");
                        break;
                    //调节步长
                    case 70:
                        SQLiteParamUtils.updateToStep(value);
                        shorts[70] = value;
                        ActSqliteUtil.insertToDBAct("Modify the Step_Size " + value + "-TCP");
                        break;
                    //防逆流控制
                    case 71:
                        SQLiteParamUtils.updateToAntiReflux(value);
                        shorts[71] = (short) value;
                        if (value == 1) {
                            ActSqliteUtil.insertToDBAct("Set anti backflow enable -TCP");
                        } else {
                            ActSqliteUtil.insertToDBAct("Set anti backflow disable -TCP");

                        }
                        break;
                    //故障清除
                    case 72:
                        controlTcp.setAddress(328);
                        controlTcp.setValue((int) value);
                        MsgQueue.writeToControlTcp(controlTcp);
                        break;
                    case 62:
                        controlTcp.setAddress(331);
                        controlTcp.setValue((int) value);
                        MsgQueue.writeToControlTcp(controlTcp);
                        break;
                    //场站最大功率设置
                    case 76:
                        SQLiteParamUtils.updateToMaxp(value);
                        shorts[76] = value;
                        ActSqliteUtil.insertToDBAct("Set the maximum power of the station " + value + "-TCP");
                        break;
                    //场站最大功率设置
                    case 77:
                        SQLiteParamUtils.updateToPvMax(value);
                        shorts[77] = value;
                        ActSqliteUtil.insertToDBAct("Set the pvmaximum power of the station " + value + "-TCP");
                        break;
                    //扩容标志
                    case 78:
                        SQLiteParamUtils.updateToExpansionMark(value);
                        shorts[78] = value;
                        ActSqliteUtil.insertToDBAct("Set ExpansionMark " + value + "-TCP");
                        break;
                    //预留电量
                    case 79:
                        SQLiteParamUtils.updateToBcm_SOCL(value);
                        shorts[79] = value;
                        ActSqliteUtil.insertToDBAct("Set BCM_SOCL " + value + "-TCP");
                        break;
                    //针对pvm光伏功率限制
                    case 80:
                        controlTcp.setSlaveId(1);
                        controlTcp.setAddress(368);
                        controlTcp.setValue((int) value);
                        MsgQueue.writeToControlPV(controlTcp);
                        break;
                    //MIN_SOC
                    case 81:
                        //检验参数的合法性
                        if (value < shorts[84]) {
                            SocUtils.updateToMinSoc(value);
                            shorts[81] = (short) value;
                        }
                        break;
                    //BCP_SOC
                    case 82:
                        //检验参数的合法性
                        if (value <= shorts[83]) {
                            SocUtils.updateToBcpSoc(value);
                            shorts[82] = (short) value;
                        }
                        break;
                    //DCM_SOC
                    case 83:
                        //检验参数的合法性
                        if (value >= shorts[82]) {
                            SocUtils.updateToDcmSoc(value);
                            shorts[83] = (short) value;
                        }
                        break;
                    //MAX_SOC
                    case 84:
                        //检验参数的合法性
                        if (value > shorts[81]) {
                            SocUtils.updateToMaxSoc(value);
                            shorts[84] = (short) value;
                        }
                        break;
                    //针对pvm光伏功率限制
                    case 85:
                        SQLiteParamUtils.updateToPv_Mode(value);
                        shorts[85] = value;
                        ActSqliteUtil.insertToDBAct("Set pv_mode " + value + "-TCP");
                        break;
                    default:
                        controlTcp.setAddress(address);
                        controlTcp.setValue((int) value);
                        MsgQueue.writeToControlTcp(controlTcp);
                        break;
                }
            }
        }
    }
}
